package com.service.impl;

import com.mapper.UserCollectionMapper;
import com.pojo.UserCollection;
import com.pojo.UserCollectionExample;
import com.service.UserCollectionService;
import com.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: HL
 * Date: 2020/3/3
 * Time: 14:34
 * Description: No Description
 */
@Service
public class UserCollectionServiceImpl implements UserCollectionService {
    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public ServerResponse save(UserCollection userCollection) {
        Byte status =1;
        userCollection.setStatus(status);
        //执行mybatis添加方法
        int rows = userCollectionMapper.insert(userCollection);
        //判断添加成功
        if (rows > 0)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }
    @Override
    public ServerResponse modifyStatus(Integer id) {
        UserCollection userCollection = userCollectionMapper.selectByPrimaryKey(id);
        Byte status = 0;
        userCollection.setStatus(status);
        int i = userCollectionMapper.updateByPrimaryKeySelective(userCollection);
        if (i>0)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }

    @Override
    public ServerResponse modify(UserCollection userCollection) {
        int i = userCollectionMapper.updateByPrimaryKeySelective(userCollection);
        if (i>0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        UserCollection userCollection = userCollectionMapper.selectByPrimaryKey(id);
        return ServerResponse.getDataSuccess(userCollection);
    }

    @Override
    public ServerResponse getAll() {
        UserCollectionExample example=new UserCollectionExample();
        UserCollectionExample.Criteria criteria = example.createCriteria();
        Byte status = 1;
        criteria.andStatusEqualTo(status);
        List<UserCollection> userCollections = userCollectionMapper.selectByExample(example);
        if (userCollections!=null&&userCollections.size()>0)
            return ServerResponse.getDataSuccess(userCollections);
        return ServerResponse.getDataFailed("没有数据");
    }
}
